﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.Filters;
using NetCoreCMS.Common;
using NetCoreCMS.IServices;
using NetCoreCMS.Models;

namespace NetCoreCMS.Web.Areas.Admin.Controllers
{
    public class User_ConfigController : ManageController
    {
        private const string WEB_VIEW = "~/Areas/Admin/Views/Users/User_Config.cshtml";
        public override void OnActionExecuting(ActionExecutingContext context)
        {
            base.OnActionExecuting(context);
            if (context.Result != null)
                return;
            ChkAdminLevel("user_config", CMSEnums.ActionEnum.View.ToString()); //检查权限
        }
        // GET: /admin/User_Config/

        public ActionResult Index()
        {
            ShowInfo();
            return View(WEB_VIEW);
        }

        [HttpPost]
        public ActionResult SubmitSave(string regrulestxt)
        {
            ChkAdminLevel("user_config", CMSEnums.ActionEnum.Edit.ToString()); //检查权限
            var model = Common.XMLConfig.Userconfig.loadConfig(_env.ContentRootPath);
            string result = string.Empty;
            try
            {
                model.regstatus = Utils.StrToInt(Request.Form["regstatus"], 0);
                model.regmsgstatus = Utils.StrToInt(Request.Form["regmsgstatus"], 0);
                model.regmsgtxt = Request.Form["regmsgtxt"];
                model.regkeywords = Request.Form["regkeywords"].FirstOrDefault().Trim();
                model.regctrl = Utils.StrToInt(Request.Form["regctrl"].FirstOrDefault().Trim(), 0);
                model.regsmsexpired = Utils.StrToInt(Request.Form["regsmsexpired"].FirstOrDefault().Trim(), 0);
                model.regemailexpired = Utils.StrToInt(Request.Form["regemailexpired"].FirstOrDefault().Trim(), 0);
                model.regverify = Request.Form["regverify"].FirstOrDefault().ToLower().IndexOf("true") >= 0 ? 1 : 0;
                model.mobilelogin = Request.Form["mobilelogin"].FirstOrDefault().ToLower().IndexOf("true") >= 0 ? 1 : 0;
                model.emaillogin = Request.Form["emaillogin"].FirstOrDefault().ToLower().IndexOf("true") >= 0 ? 1 : 0;
                model.regrules = Request.Form["regrules"].FirstOrDefault().ToLower().IndexOf("true") >= 0 ? 1 : 0;
                model.regrulestxt = regrulestxt;

                model.invitecodeexpired = Utils.StrToInt(Request.Form["invitecodeexpired"].FirstOrDefault().Trim(), 1);
                model.invitecodecount = Utils.StrToInt(Request.Form["invitecodecount"].FirstOrDefault().Trim(), 0);
                model.invitecodenum = Utils.StrToInt(Request.Form["invitecodenum"].FirstOrDefault().Trim(), 0);
                model.pointcashrate = Utils.StrToDecimal(Request.Form["pointcashrate"].FirstOrDefault().Trim(), 0);
                model.pointinvitenum = Utils.StrToInt(Request.Form["pointinvitenum"].FirstOrDefault().Trim(), 0);
                model.pointloginnum = Utils.StrToInt(Request.Form["pointloginnum"].FirstOrDefault().Trim(), 0);

                model.tixian_switch = Utils.StrToInt(Request.Form["tixian_switch"], 0);
                model.tixian_week = CMSRequest.GetFormString("tixian_week", HttpContext);
                model.tixian_base = CMSRequest.GetFormDecimal("tixian_base", 100, HttpContext);
                model.tixian_start = CMSRequest.GetFormDecimal("tixian_start", 100,HttpContext);
                model.tixian_chongxiao = CMSRequest.GetFormDecimal("tixian_chongxiao", 100,HttpContext);
                model.tixian_fee = CMSRequest.GetFormDecimal("tixian_fee", 100,HttpContext);

                model.subscribe = Utils.StrToInt(Request.Form["subscribe"], 0);
                model.danliang = CMSRequest.GetFormDecimal("danliang", 10000, HttpContext);

                model.transfer = CMSRequest.GetFormInt("transfer", 1,HttpContext);
                model.team = CMSRequest.GetFormInt("team", 1,HttpContext);
                model.cash2amount = CMSRequest.GetFormInt("cash2amount", 1,HttpContext);
                model.shop2amount = CMSRequest.GetFormInt("shop2amount", 0,HttpContext);
                model.passshare = CMSRequest.GetFormInt("passshare", 0,HttpContext);

                Common.XMLConfig.Userconfig.saveConifg(model,_env.ContentRootPath);

                AddAdminLog(CMSEnums.ActionEnum.Edit.ToString(), "修改用户配置信息"); //记录日志
                result=JscriptMsg("修改用户配置成功！", "index");
            }
            catch
            {
                result=JscriptMsg("文件写入失败，请检查是否有权限！", "back");
            }
            ViewData["model"] = model;
            ViewBag.ClientScript = result;
            return View(WEB_VIEW);
        }

        #region 赋值操作=================================
        private void ShowInfo()
        {
            var model = Common.XMLConfig.Userconfig.loadConfig(_env.ContentRootPath);
            ViewData["model"] = model;
        }
        #endregion
    }
}
